package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.MajorMapper;
import com.kgc.entity.Major;
import com.kgc.entity.MajorExample;
import com.kgc.entity.TreeNode;
import com.kgc.entity.TreeResult;
import com.kgc.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
   private MajorMapper majorMapper;

    public PageInfo<Major> findAll(int page, int limit, Major major) {
        MajorExample example=new MajorExample();
        MajorExample.Criteria criteria = example.createCriteria();
        if (major.getCid()!=null&&major.getCid()!=-1){
        criteria.andCidEqualTo(major.getCid());
        }
        if (major.getMname()!=null&&major.getMname()!=""){
            criteria.andMnameLike("%"+major.getMname()+"%");
        }
        PageHelper.startPage(page,limit);
        List<Major> list = majorMapper.selectByExample(example);
        PageInfo<Major> pb=new PageInfo<>(list);
        return pb;
    }

    @Override
    public void save(Major major) {
        majorMapper.insertSelective(major);
    }

    @Override
    public void delete(Integer id) {
        majorMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deletes(int[] ids) {
      /*  for (int i=0;i<ids.length;i++){
            majorMapper.deleteByPrimaryKey(ids[i]);
        }*/
      //方法二：在majorMpper.xml文件中添加一个删除方法，并给majorMpper添加该抽象方法
        majorMapper.deleteMajor(ids);
    }

    @Override
    public Major findById(Integer id) {
        Major major= majorMapper.selectByPrimaryKey(id);
        return major;
    }

    @Override
    public void update( Major major) {
        majorMapper.updateByPrimaryKeySelective(major);
    }



    @Override
    public void saveMajor(Major major) {
        majorMapper.insert(major);
    }

    @Override
    public List<TreeNode> findDetps() {

        return majorMapper.findDetps();
    }

    @Override
    public List<TreeResult> findTreeResult() {
        return majorMapper.findTreeResult();
    }

}
